/*
* Copyright 2016 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-wechat-open-api
*
* @File name : WechatAuthorityController.java
*
* @Author : LiuJun
*
* @Date : 2016年11月28日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2016年11月28日    LiuJun    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmcc.gateway.wechat.controller.authority;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmcc.gateway.wechat.common.ApiVersionConsts;
import com.yonyou.gmcc.gateway.wechat.service.interfaces.AuthorityApiServiceInterface;

/**
*
* @author LiuJun
* 微信授权controller
* @date 2016年11月28日
*/
@RestController
@RequestMapping(value = ApiVersionConsts.BASE_PATH+"/authorization" )
public class WechatAuthorityController {
    
    private static Logger logger=Logger.getLogger(WechatAuthorityController.class);
    @Autowired
    private AuthorityApiServiceInterface authorityApiServiceInterface;
    
    
    
//    /**
//    *
//    * @author LiuJun
//    * 接收第三方平台授权ticket
//    * @date 2016年11月28日
//    * @param nonce
//    * @param timestamp
//    * @param encryptType
//    * @param msgSignature
//    * @param xml
//    * @throws Exception
//    */
//    @RequestMapping(value = "/tickets", method =  RequestMethod.POST, consumes = "text/plain;charset=UTF-8")
//    public void saveTicket(@RequestParam("nonce") String nonce,
//                           @RequestParam("timestamp") String timestamp,
//                           @RequestParam("encrypt_type") String encryptType,
//                           @RequestParam("msg_signature") String msgSignature,
//                           @RequestBody String xml) throws Exception{
//        
//        logger.info("接收第三方平台授权ticket-saveTicket is entry");
//        authorityApiServiceInterface.saveTicket(nonce, timestamp, encryptType, msgSignature, xml);
//        
//    }
    
    
    /**
    *
    * @author LiuJun
    * 生成授权登录URL-点击授权之后触发
    * @date 2016年11月28日
    * @param dealerId
    * @return
    * @throws Exception
    */
    @RequestMapping(value = "/loginURL/{dealerId}",method =  RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public String getAuthorityURL(@PathVariable("dealerId") String dealerId) throws Exception{
        logger.info("生成授权登录URL-getAuthorityURL is entry");

        return authorityApiServiceInterface.getAuthorityURL(dealerId);
        
    }
    
    
    /**
    *
    * @author LiuJun
    * 授权后-回调URL-其中参数授权码和过期日期 微信服务器在回调时会自动加上
    * @date 2016年11月28日
    * @param authCode
    * @param expired
    * @param dealerId
    * @throws Exception
    */
    @RequestMapping(value = "/callback/{dealerCode}",method =  RequestMethod.GET)
    public void receiveAuthorization(@RequestParam("auth_code") String authCode,
                                       @RequestParam("expires_in") Long expired,
                                       @PathVariable("dealerCode") String dealerCode) throws Exception{
        
        logger.info("授权后-回调URL-receiveAuthorization is entry");
        authorityApiServiceInterface.receiveAuthorization(authCode, expired, dealerCode);
    }
    
    
    /**
    *
    * @author LiuJun
    * 根据授权方appid获取token信息
    * @date 2016年11月29日
    * @param authorizerAppid
    * @return
    * @throws Exception
    */
    @RequestMapping(value = "/tokens/{authorizerAppid}",method =  RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public String getAccessToken(@PathVariable("authorizerAppid") String authorizerAppid) throws Exception{
        String authorizerAccessToken = authorityApiServiceInterface.getAccessToken(authorizerAppid); 
        return authorizerAccessToken;
    }
    
    
    /**
    *
    * @author LiuJun
    * 刷新token
    * @date 2016年12月19日
    * @throws Exception
    */
    @RequestMapping(value = "/refreshTokens",method =  RequestMethod.GET)
    public void refreshAccessToken() throws Exception{
        logger.info("refreshAccessToken is start");
        authorityApiServiceInterface.refreshAccessToken();
        logger.info("refreshAccessToken is end");
    }
    
    /**
    *
    * @author LiuJun
    * 刷新ComponentAccessToken
    * @date 2016年12月19日
    * @throws Exception
    */
    @RequestMapping(value = "/refreshComponentAccessToken",method =  RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public String refreshComponentAccessToken() throws Exception{
        logger.info("refreshComponentAccessToken is start");
        String componentAccessToken = authorityApiServiceInterface.refreshComponentAccessToken();
        logger.info("refreshComponentAccessToken is end");
        return componentAccessToken;
    }





}
